import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';

class BuglyPlugin {
  static const MethodChannel _channel =
      const MethodChannel('bugly_plugin');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static void postDartError(String detail, String stack){
    if(Platform.isAndroid){
      Map map = Map();
      map['message'] = detail;
      map['stack'] = stack;
      _channel.invokeMethod('postDartError', map);
    }else if(Platform.isIOS){
      try {
        Map map = Map();
        map['exception'] = detail;
        map['stack'] = null;
        //stack 转为map
        print('BuglyPlugin.postDartError stack:\n$stack');
        List<String> stackList = stack.split("\n");
        if(stackList != null && stackList.length > 0){
          map['stack'] = stackList;
        }
        _channel.invokeMethod('postDartError', map);
      }catch(e){
        print('catchException e:${e.toString()}');
      }
    }
  }

  static void postTestError() {
    _channel.invokeMethod('postTestError');
  }
  static void postANRError() {
    if(Platform.isAndroid) {
      _channel.invokeMethod('postANRError');
    }
  }
  static void postNativeError() {
    if(Platform.isAndroid) {
      _channel.invokeMethod('postNativeError');
    }
  }
  static void checkUpdate(){
    if(Platform.isAndroid) {
      _channel.invokeMethod('checkUpdate');
    }
  }
}
